package java8.lambda.demo03;

import java8.lambda.demo01.Apple;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;

/**
 * @author ChangLiang
 * @date 2020/4/22
 */
public class AppleFilter {

    public static List<Apple> findApple(List<Apple> apples, Predicate<Apple> predicate) {
        List<Apple> list = new ArrayList<>();

        for (Apple apple : apples) {
            if (predicate.test(apple)) {
                list.add(apple);
            }
        }
        return list;
    }

    public static List<Apple> findAppleByWeight(List<Apple> apples, LongPredicate predicate) {
        List<Apple> list = new ArrayList<>();

        for (Apple apple : apples) {
            if (predicate.test(apple.getWeight())) {
                list.add(apple);
            }
        }
        return list;
    }

    public static List<Apple> findAppleByColorAndWeight(List<Apple> apples, BiPredicate<String, Long> predicate) {
        List<Apple> list = new ArrayList<>();

        for (Apple apple : apples) {
            if (predicate.test(apple.getColor(),apple.getWeight())) {
                list.add(apple);
            }
        }
        return list;
    }
}
